<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 基于构造器实现继承
      // 人类
      function Person(name, age) {
        this.name = name;
        this.age = age;
      }

      Person.prototype.sayHi = function () {
        console.log(`大家好，我是${this.name}，我今年${this.age}岁了`);
      };

      let p1 = new Person("A", 25);
      console.log(p1.name, p1.age);
      p1.sayHi();

      //   学生类
      function Student(name, age, price) {
        Person.call(this, name, age);
        this.price = price;
      }
      //   将学生类的原型对象指向人类的原型对象
      Student.prototype = new Person();
      //   将学生类的原型对象的constructor属性指向Student
      Student.prototype.constructor = Student;
      console.log(Student.prototype.constructor);

      //   方法继承重写
      Student.prototype.sayHi = function () {
        console.log(
          `大家好，我是${this.name}，我今年${this.age}岁了,我的收入是${this.price}`
        );
      };
      let s1 = new Student("B", 26, 15000);
      s1.sayHi();

      //   教师类
      function Teacher(name, age, course) {
        Person.call(this, name, age);
        this.course = course;
      }

      Teacher.prototype = new Person();
      Teacher.prototype.constructor = Teacher;
      Teacher.prototype.sayHi = function () {
        console.log(
          `大家好，我是${this.name}，我今年${this.age}岁了,我教的课是${this.course}`
        );
      };
      console.log(Teacher.prototype.constructor);
      let t1 = new Teacher("C", 30, "js");
      t1.sayHi();
    </script>

    <script>
      // 基于类实现继承
      class Person1 {
        constructor(name, age) {
          this.name = name;
          this.age = age;
        }
        sayHi() {
          console.log(`我是${this.name},年龄${this.age}`);
        }
      }

      let p2 = new Person1("D", 25);
      p2.sayHi();

      class Student1 extends Person1 {
        constructor(name, age, price) {
          super(name, age);
          this.price = price;
        }
        sayHi() {
          console.log(`我是${this.name},年龄${this.age},月入${this.price}`);
        }
      }

      let s2 = new Student1("E", 29, 1000);
      s2.sayHi();
    </script>
  </body>
</html>
